﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class UStack<T> : IEnumerable<T>
    {
        List<T> data;
        int top;
        const int defaultSize = 4;

        public T this[int index]
        {
            get { return data[index]; }
            set { data[index] = value; }
        }

        public UStack(int size = defaultSize)
        {
            data = new List<T>(size);
            top = -1;
        }
        public int Count
        {
            get { return data.Count; }
        }

        public void Clear()
        {
            top = -1;
            data = new List<T>(defaultSize);
        }
        public bool IsEmpty()
        {
            return top == -1;
        }
        public void Push(T e)
        {
            ++top;
            data.Add(e);
        }
        public T Pop()
        {
            T temp = default(T);
            if (IsEmpty())
            {
                return default(T);
            }

            temp = data[top];
            data.RemoveAt(top);
            top--;
            return temp;
        }
        public T Peek()
        {
            if (IsEmpty())
            {
                return default(T);
            }
            return data[top];
        }

        public IEnumerator<T> GetEnumerator()
        {
            return data.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return data.GetEnumerator();
        }
    }
}
